package com.lxy.cxcyxmmsbackend.sys.service.impl;

import com.lxy.cxcyxmmsbackend.sys.entity.QxglDz;
import com.lxy.cxcyxmmsbackend.sys.exceptionHandle.exce.NormalException;
import com.lxy.cxcyxmmsbackend.sys.mapper.CommonMapper;
import com.lxy.cxcyxmmsbackend.sys.mapper.QxglDzMapper;
import com.lxy.cxcyxmmsbackend.sys.res.Result;
import com.lxy.cxcyxmmsbackend.sys.service.QxglDzService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 刘新颖
 * @since 2024-01-07
 */
@Service
public class QxglDzServiceImpl extends ServiceImpl<QxglDzMapper, QxglDz> implements QxglDzService {

    @Resource
    QxglDzMapper qxglDzMapper;
    @Resource
    CommonMapper commonMapper;

    @Override
    @Transactional
    public Result selectQxglDzByCdBh(String cdBh) {
        List<QxglDz> qxglDzList = qxglDzMapper.selectQxglDzByCdBh(cdBh);
        if (qxglDzList == null || qxglDzList.size() == 0) {
            throw new NormalException("查询动作失败");
        }
        return Result.success("查询动作成功", qxglDzList);
    }


    @Override
    @Transactional
    public Result insertQxglDz(QxglDz qxglDz) {
        String newBhAct = commonMapper.getNewBhAct();
        qxglDz.setDzBh(newBhAct);
        qxglDz.setDzCjsj(new Date(System.currentTimeMillis()));
        int i = qxglDzMapper.insertQxglDz(qxglDz);
        if (i != 1) {
            throw new NormalException("新增动作失败");
        }
        return Result.success("新增动作成功", qxglDz);
    }

    @Override
    @Transactional
    public Result updateQxglDz(QxglDz qxglDz) {
        qxglDz.setDzGxsj(new Date());
        int i = qxglDzMapper.updateQxglDz(qxglDz);
        if (i != 1) {
            throw new NormalException("修改动作失败");
        }
        return Result.success("修改动作成功", qxglDz);
    }

    @Override
    @Transactional
    public Result deleteQxglDz(String dzBh) {
        int i = qxglDzMapper.deleteQxglDz(dzBh);
        if (i != 1) {
            throw new NormalException("删除动作失败");
        }

        return Result.success("修删除动作成功", dzBh);
    }
}
